# 1. 基本响应方式:  返回字符串（自动转为HTML响应）
# 2. 基本响应方式:  返回HTML模板（常用）


from flask import Flask,request,render_template
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World!'

@app.route('/login', methods=['POST'])
def login():
    #使用users来模拟数据库
    users = [
        {"name":"damu","password":"123"},
        {"name":"zdy","password":"789"},
        {"name":"admin","password":"567"},
        {"name":"test","password":"anc"},
        {"name":"boss","password":"xdf"}
    ]

    #获取浏览器端传递过来的数据
    username = request.form.get('username')
    password = request.form.get('password')

    #链接数据库 在数据库中查找是否有username对应的用户
    def user_filter(user):
       return user.get("name") == username

    new_users = filter(user_filter, users)

    new_users = list(new_users)

    if len(new_users) == 1:
        #代表登录的用户存在于数据库中 接下来要查密码
        if new_users[0].get("password") == password:
            # 代表登录成功
            return render_template("success.html",username = username )
        else:
            # 代表密码有问题
            return render_template("password_fail.html")
    else:
        # 代表登录的用户不存在于数据库中
        return render_template("fail.html")

if __name__ == '__main__':
    app.run(debug=True)

